package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.*;
import com.zb.mapper.ZjNetworkEmpAuthenFieldMapper;
import com.zb.result.PageUtil;
import com.zb.service.ZjNetworkEmpAuthenFieldContentService;
import com.zb.service.ZjNetworkEmpAuthenFieldService;
import com.zb.vo.NetworkAuthenFieldListVo;
import com.zb.vo.NetworkAuthenFieldSelectVo;
import com.zb.vo.NetworkAuthenFieldVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 网点人员认证字段表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-01-31
 */
@Service
public class ZjNetworkEmpAuthenFieldServiceImpl extends ServiceImpl<ZjNetworkEmpAuthenFieldMapper, ZjNetworkEmpAuthenField> implements ZjNetworkEmpAuthenFieldService {
    @Resource
    private ZjNetworkEmpAuthenFieldMapper networkEmpAuthenFieldMapper;
    @Resource
    private ZjNetworkEmpAuthenFieldContentService networkEmpAuthenFieldContentService;

    // 保存或更新认证字段
    @Override
    @Transactional
    public void save(Long custAccId, NetworkAuthenFieldVo authenFieldVo) {
        Assert.isNull(networkEmpAuthenFieldMapper.existByName(custAccId, authenFieldVo.getId(), authenFieldVo.getName()), "认证字段名称已重复："+ authenFieldVo.getName());
        ZjNetworkEmpAuthenField authenField = new ZjNetworkEmpAuthenField();
        BeanUtil.copyProperties(authenFieldVo, authenField);
        authenField.setDisabled(authenFieldVo.getStatus());
        if (null != authenField.getId()) {
            authenField.setType(null);
            this.updateById(authenField);
        } else {
            authenField.setId(IdWorker.getId());
            authenField.setCustAccId(custAccId);
            this.save(authenField);
        }
        if (CollectionUtil.isNotEmpty(authenFieldVo.getDelContentIds())) {
            networkEmpAuthenFieldContentService.removeByIds(authenFieldVo.getDelContentIds());
        }
        networkEmpAuthenFieldContentService.save(custAccId, authenField.getId(), authenFieldVo.getContents());
    }

    // 获取认证字段列表
    @Override
    public Map<String, Object> getList(Long custAccId, SelectVo selectVo) {
        PageUtil.setPageLimit(selectVo);
        QueryWrapper<ZjNetworkEmpAuthenField> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name", "type", "tip", "sort", "is_required AS required", "is_disabled AS disabled", "is_mobile_show AS mobileShow", "gmt_create", "gmt_update")
                .eq("cust_acc_id", custAccId)
                .orderByDesc("gmt_update");
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name", selectVo.getName());
        }
        Page<ZjNetworkEmpAuthenField> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);
        List<ZjNetworkEmpAuthenFieldContent> contentList = networkEmpAuthenFieldContentService.getByNetworkEmpAuthenFieldIds(page.getRecords().stream().map(ZjNetworkEmpAuthenField::getId).toList());
        List<NetworkAuthenFieldListVo> voList = page.getRecords().stream().map(authenField -> {
            NetworkAuthenFieldListVo vo = new NetworkAuthenFieldListVo();
            BeanUtil.copyProperties(authenField, vo);
            vo.setStatus(authenField.getDisabled());
            vo.setCreateTime(authenField.getGmtCreate());
            vo.setUpdateTime(authenField.getGmtUpdate());
            vo.setContent(contentList.stream().filter(f -> Objects.equals(f.getNetworkEmpAuthenFieldId(), authenField.getId())).map(ZjNetworkEmpAuthenFieldContent::getVal).collect(Collectors.joining("、")));
            return vo;
        }).toList();
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }

    // 根据认证字段ID获取认证字段详情
    @Override
    public NetworkAuthenFieldVo getDetail(Long id) {
        ZjNetworkEmpAuthenField authenField = this.getById(id);
        Assert.notNull(authenField, "认证字段参数[ "+ id +" ]的数据不存在");
        NetworkAuthenFieldVo vo = new NetworkAuthenFieldVo();
        BeanUtil.copyProperties(authenField, vo);
        vo.setStatus(authenField.getDisabled());
        vo.setContents(networkEmpAuthenFieldContentService.getByNetworkEmpAuthenFieldId(id).stream().map(con -> {
            return new FieldContentVo(con.getId(), con.getVal());
        }).toList());
        return vo;
    }

    // 批量启用或禁用认证字段
    @Override
    public void updateDisabledBatchById(List<Long> ids, boolean status) {
        if (CollectionUtil.isEmpty(ids)) {
            return;
        }
        networkEmpAuthenFieldMapper.updateDisabledBatchById(ids, status);
    }

    // 批量删除认证字段
    @Override
    @Transactional
    public void deleteBatchById(List<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            return;
        }
        networkEmpAuthenFieldContentService.deleteBatchByNetworkEmpAuthenFieldIds(ids);
        this.removeByIds(ids);
    }

    // 根据商家ID获取工程师认证字段
    @Override
    public List<NetworkAuthenFieldSelectVo> getNetworkAuthenField(Long custAccId) {
        QueryWrapper<ZjNetworkEmpAuthenField> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name", "type", "upload_size", "upload_qty", "tip", "is_required AS required")
                .eq("cust_acc_id", custAccId)
                .eq("is_disabled", false)
                .eq("is_mobile_show", true)
                .orderByAsc("sort", "gmt_update");
        List<ZjNetworkEmpAuthenField> authenFieldList = this.list(wrapper);
        if (CollectionUtil.isEmpty(authenFieldList)) {
            return new ArrayList<>();
        }
        List<ZjNetworkEmpAuthenFieldContent> contentList = networkEmpAuthenFieldContentService.getByNetworkEmpAuthenFieldIds(authenFieldList.stream().map(ZjNetworkEmpAuthenField::getId).toList());
        return authenFieldList.stream().map(authenField -> {
            NetworkAuthenFieldSelectVo vo = new NetworkAuthenFieldSelectVo();
            BeanUtil.copyProperties(authenField, vo);
            vo.setStrs(contentList.stream().filter(f -> Objects.equals(f.getNetworkEmpAuthenFieldId(), authenField.getId())).map(ZjNetworkEmpAuthenFieldContent::getVal).toList());
            return vo;
        }).toList();
    }
}
